package com.catt.orderdinner.controller;

import com.catt.orderdinner.dataobject.TbUser;
import com.catt.orderdinner.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/admin")
public class AdminLoginController {

    @Autowired
    private UserServiceImpl userService;


    @RequestMapping("login")
    public String login(HttpServletRequest request, String username , String password, Model model) {
        String method = request.getMethod();
        if (method.equals("GET")) {
            return "admin/login";
        }else {
            TbUser user = userService.login(username,password);
            //验证用户名和密码
            if (user!=null){
                request.getSession().setAttribute("user",user);
                model.addAttribute("user",user);
                return "redirect:/admin/index";
            }else {
                model.addAttribute("msg","用户名或者密码错误，请重新登陆");
                return "admin/login";
            }
        }
    }

    @RequestMapping("logout")
    public String logout(Model model,HttpServletRequest request){
        request.getSession().removeAttribute("user");
        model.addAttribute("msg","欢迎您再次使用本系统");
        return "admin/login";
    }

    @RequestMapping("index")
    public String index(){
        return "admin/index";
    }

    @RequestMapping("welcome")
    public String welcome(){
        return "admin/welcome";
    }
}
